#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int main() {
	int i;
	int seed = 1000;
	int numPlayer = 2;
	int maxBonus = 10;
	int p, r, handCount;
	int bonus;
	int numActions = 0;
	int numGold = 0;
	int foundTwoTreasureMaps = 0;
	int k[10] = {adventurer, council_room, feast, gardens, mine
	             , remodel, smithy, village, baron, great_hall};
	struct gameState G;
	int maxHandCount = 5;
	// arrays of all coppers, silvers, and golds
	int coppers[MAX_HAND];
	int silvers[MAX_HAND];
	int golds[MAX_HAND];
	for (i = 0; i < MAX_HAND; i++)
		{
			coppers[i] = copper;
			silvers[i] = silver;
			golds[i] = gold;
		}

	printf ("TESTING card effect treasure_map:\n");

	for (p = 0; p < numPlayer; p++) {
		for (handCount = 1; handCount <= maxHandCount; handCount++) {

#if (NOISY_TEST == 1)
			printf("Test player %d with %d card(s) in hand only 1 TM\n", p, handCount);
#endif
			memset(&G, 23, sizeof(struct gameState));   // clear the game state
			r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
			G.handCount[p] = handCount;                 // set the number of cards on hand
			memcpy(G.hand[p], coppers, sizeof(int) * handCount); // set all the cards to copper

			G.hand[p][0] = treasure_map;
			numGold = fullDeckCount(p, gold, &G);
			G.whoseTurn = p;

			printf("before G.hand[p][0] = %d\n", G.hand[p][0]);

			foundTwoTreasureMaps = cardEffect(treasure_map, 0, 0, 0, &G, 0, 0);
#if (NOISY_TEST == 1)
			printf("after G.hand[p][0] = %d\n", G.hand[p][0]);
			printf("handCount = %d\n", G.handCount[p]);
#endif
			if (foundTwoTreasureMaps == 1) {
				assert(G.handCount[p] == handCount - 2);
				assert(fullDeckCount(p, gold, &G) == numGold + 4); // check if card was successful
			} else {
				assert(G.handCount[p] == handCount);
				assert(fullDeckCount(p, gold, &G) == numGold);
			}
		}
	}


	for (p = 0; p < numPlayer; p++) {
		for (handCount = 2; handCount <= maxHandCount; handCount++) {

#if (NOISY_TEST == 1)
			printf("Test player %d with %d card(s) in hand and 2 TM\n", p, handCount);
#endif
			memset(&G, 23, sizeof(struct gameState));   // clear the game state
			r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
			G.handCount[p] = handCount;                 // set the number of cards on hand
			memcpy(G.hand[p], coppers, sizeof(int) * handCount); // set all the cards to copper

			G.hand[p][0] = treasure_map;
			G.hand[p][1] = treasure_map;
			numGold = fullDeckCount(p, gold, &G);
			G.whoseTurn = p;

			printf("before G.hand[p][0] = %d [p][1] = %d\n", G.hand[p][0], G.hand[p][1]);

			foundTwoTreasureMaps = cardEffect(treasure_map, 0, 0, 0, &G, 0, 0);
#if (NOISY_TEST == 1)
			printf("after G.hand[p][0] = %d [p][1] = %d\n", G.hand[p][0], G.hand[p][1]);
			printf("handCount = %d\n", G.handCount[p]);
#endif
			if (foundTwoTreasureMaps == 1) {
				assert(G.handCount[p] == handCount - 2);
				assert(fullDeckCount(p, gold, &G) == numGold + 4); // check if card was successful
			} else {
				assert(G.handCount[p] == handCount);
				assert(fullDeckCount(p, gold, &G) == numGold);
			}
		}
	}

	printf("All tests passed!\n");
	
	return 0;
}
